package com.fuxian.action;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fuxian.model.Notice;
import com.fuxian.model.User;
import com.fuxian.service.UserService;
import com.fuxian.util.ReturnMapUtil;
import com.sun.org.apache.bcel.internal.generic.RETURN;

@Controller
@Scope("prototype")
public class UserAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(UserAction.class);

	@Resource
	private UserService userService;
	private String user_name;
	private String password;
	private String phone_no;
	private String qq_no;
	private String name;
	private String sex;
	private String age;
	private String school;
	private String grade;
	private String clazz;
	private String email;
	private String tel_no;
	private String address;
	private String photo_path;

	private User user;
	private String password_new;
	private int rows;
	private int page;

	public void setRows(int rows) {
		this.rows = rows;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setPassword_new(String passwordNew) {
		password_new = passwordNew;
	}

	public User getUser() {
		return user;
	}

	public void setUser_name(String userName) {
		user_name = userName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setPhone_no(String phoneNo) {
		phone_no = phoneNo;
	}

	public void setQq_no(String qqNo) {
		qq_no = qqNo;
	}

	public void setTel_no(String telNo) {
		tel_no = telNo;
	}

	public void setPhoto_path(String photoPath) {
		photo_path = photoPath;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public void setSchool(String school) {
		this.school = school;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	public void setClazz(String clazz) {
		this.clazz = clazz;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * 登录
	 * 
	 * @return
	 */
	public String login() {
		log.debug(user_name + ":" + password);
		returnMap = new HashMap<String, Object>();
		if (user_name != null && !user_name.equals("") && password != null
				&& !password.equals("")) {
			if (userService.login(user_name, password)) {
				returnMap.put("status", true);
				returnMap.put("msg", "登录成功！");
				session.put("user_name", user_name);

			} else {
				returnMap.put("status", false);
				returnMap.put("msg", "登录失败！");

			}
		} else {
			returnMap.put("status", false);
			returnMap.put("msg", "用户名或密码为空！");
		}
		return SUCCESS;
	}

	/**
	 * 退出
	 * 
	 * @return
	 */
	public String logout() {
		returnMap = new HashMap<String, Object>();
		session.remove("user_name");
		returnMap.put("status", true);
		returnMap.put("msg", "退出成功！");
		return SUCCESS;
	}

	/**
	 * 检查是否存在某一个用户
	 * 
	 * @return
	 */
	public String isExistUser() {
		log.debug(user_name);
		returnMap = new HashMap<String, Object>();
		if (user_name != null && !user_name.equals("")) {
			if (userService.isExistUser(user_name)) {
				returnMap.put("status", true);
				returnMap.put("msg", "该用户已存在！");
			} else {
				returnMap.put("status", false);
				returnMap.put("msg", "可以使用！");
			}
		} else {
			returnMap.put("status", true);
			returnMap.put("msg", "用户名为空！");
			log.debug("user_name is null");
		}
		return SUCCESS;
	}

	/**
	 * 注册
	 * 
	 * @return
	 */
	public String register() {
		returnMap = new HashMap<String, Object>();
		if (user_name != null && !user_name.equals("") && password != null
				&& !password.equals("")) {
			if (userService.register(user_name, password, phone_no, qq_no)) {
				returnMap.put("status", true);
				returnMap.put("msg", "注册成功！");
			} else {
				returnMap.put("status", false);
				returnMap.put("error", "未知错误");
			}
		} else {
			returnMap.put("status", false);
			returnMap.put("error", "用户名或密码为空！");
		}
		return SUCCESS;
	}

	/**
	 * 修改密码
	 * 
	 * @return
	 */
	public String changePwd() {
		returnMap = new HashMap<String, Object>();
		// user_name,password,password_new not null
		if (user_name != null && !user_name.equals("") && password != null
				&& !password.equals("") && password_new != null
				&& !password_new.equals("")) {

			// session is include user_name && session's user_name equle
			// user_name
			if (session.get("user_name") != null
					&& session.get("user_name").equals(user_name)) {

				// is exist this user by user_name & password
				User user = new User();
				user.setUser_name(user_name);
				user.setPassword(password);
				if (userService.selectUserByUserNamePwd(user) != null) {

					if (userService.changPwd(user_name, password)) {
						returnMap = ReturnMapUtil.setReturnHeader(returnMap,
								true, "修改成功！");
					} else {
						returnMap = ReturnMapUtil.setReturnHeader(returnMap,
								false, "修改失败！");
					}
				} else {
					returnMap = ReturnMapUtil.setReturnHeader(returnMap, false,
							"非法操作！");
				}
			} else {
				returnMap = ReturnMapUtil.setReturnHeader(returnMap, false,
						"非法操作！");
			}

		} else {
			returnMap = ReturnMapUtil.setReturnHeader(returnMap, false,
					"用户名或者密码为空！");
		}
		return SUCCESS;
	}

	/**
	 * 获取用户个人信息
	 * 
	 * @return
	 */
	public String getUserInfo() {
		returnMap = new HashMap<String, Object>();
		if (user_name != null && !user_name.equals("")) {
			if (session.get("user_name") != null
					&& session.get("user_name").equals(user_name)) {
				User user = userService.selectUserByUserName(user_name);
				// returnMap.put(User.user_name_ATTR, user.getuser_name());
				// returnMap.put(User.name_ATTR, user.getname());
				// returnMap.put(User.sex_ATTR, user.getsex());
				// returnMap.put(User.age_ATTR, user.getage());
				// returnMap.put(User.school_ATTR, user.getschool());
				// returnMap.put(User.grade_ATTR, user.getgrade());
				// returnMap.put(User.clazz_ATTR, user.getClass());
				// returnMap.put(User.email_ATTR, user.getemail());
				// returnMap.put(User.phone_no_ATTR, user.getphone_no());
				// returnMap.put(User.tel_no_ATTR, user.gettel_no());
				// returnMap.put(User.address_ATTR, user.getaddress());
				// returnMap.put(User.qq_num_ATTR, user.getqq_num());
				// returnMap.put(User.last_in_time_ATTR, DateUtile
				// .timeStampToString(user.getlast_in_time()));
				// returnMap.put(User.modify_time_ATTR, DateUtile
				// .timeStampToString(user.getmodify_time()));
				// returnMap.put(User.photo_path_ATTR, user.getphoto_path());
				returnMap.put("user_info", user);
			} else {
				returnMap = ReturnMapUtil.setReturnHeader(returnMap, false,
						"非法用户！");
			}
		} else {
			returnMap = ReturnMapUtil.setReturnHeader(returnMap, false,
					"用户名为空！");
		}
		return SUCCESS;
	}

	/**
	 * 修改用户个人信息
	 * 
	 * @return
	 */
	public String changeUserInfo() {
		returnMap = new HashMap<String, Object>();
		if (user_name != null && !user_name.equals("")) {
			if (session.get("user_name") != null
					&& session.get("user_name").equals(user_name)) {
				User user = new User();
				user.setUser_name(user_name);
				user.setPhone_no(phone_no);
				user.setQq_num(qq_no);
				user.setName(name);
				user.setSex(sex);
				user.setAge(Integer.parseInt(age));
				user.setSchool(school);
				user.setGrade(grade);
				user.setClazz(clazz);
				user.setEmail(email);
				user.setTel_no(tel_no);
				user.setAddress(address);
				user.setPhoto_path(photo_path);
				userService.modifyUser(user);
				returnMap = ReturnMapUtil.setReturnHeader(returnMap, true,
						"个人信息修改成功！");

			} else {
				returnMap = ReturnMapUtil.setReturnHeader(returnMap, false,
						"非法用户！");
			}
		} else {
			returnMap = ReturnMapUtil.setReturnHeader(returnMap, false,
					"用户名为空！");
		}
		return SUCCESS;
	}

	/**
	 * 获取全部用户
	 * 
	 * @return
	 */
	public String getUserList() {
		returnMap = new HashMap<String, Object>();
		if (user_name != null && !user_name.equals("")
				&& session.get("user_name") != null) {
			page = ((page == 0) ? 1 : page);
			rows = ((rows == 0) ? 1 : rows);
			int totalCount = userService.selectCount();
			int max_page = (totalCount + rows - 1) / rows;
			returnMap.put("max_page", max_page);
			returnMap.put("page", page + 1);
			returnMap.put("total", totalCount);
			returnMap.put("rows", userService.selectAllUsers(rows * page - 1,
					rows));

			returnMap = ReturnMapUtil.setReturnHeader(returnMap, true,
					"获取全部用户成功！");
		} else {
			returnMap = ReturnMapUtil
					.setReturnHeader(returnMap, false, "非法用户！");
		}
		return SUCCESS;
	}

	/**
	 * 测试
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// Map<String, Object> returnMap = new HashMap<String, Object>();
		// // returnMap.put("status", false);
		// // returnMap.put("error", "用户名或密码为空！");
		// List<RUser> userList = new ArrayList<RUser>();
		// StringBuffer returnUserSb = new StringBuffer();
		// RUser user = null;
		// for (int i = 0; i < 10; i++) {
		// user = new RUser("fuxian", "付显", "jinongyizhong", 23, i + "", "",
		// "", "", "", "", "", "", null, null, "");
		// userList.add(user);
		// // returnMap.put(User.user_name_ATTR, user.getuser_name());
		// // returnMap.put(User.name_ATTR, user.getname());
		// // returnMap.put(User.sex_ATTR, user.getsex());
		// // returnMap.put(User.age_ATTR, user.getage());
		// // returnMap.put(User.school_ATTR, user.getschool());
		// // returnMap.put(User.grade_ATTR, user.getgrade());
		// // returnMap.put(User.clazz_ATTR, user.getclazz());
		// // returnMap.put(User.email_ATTR, user.getemail());
		// // returnMap.put(User.phone_no_ATTR, user.getphone_no());
		// // returnMap.put(User.tel_no_ATTR, user.gettel_no());
		// // returnMap.put(User.address_ATTR, user.getaddress());
		// // returnMap.put(User.qq_num_ATTR, user.getqq_num());
		// // // returnMap.put(User.last_in_time_ATTR, DateUtile
		// // // .timeStampToString(user.getlast_in_time()));
		// // // returnMap.put(User.modify_time_ATTR, DateUtile
		// // // .timeStampToString(user.getmodify_time()));
		// // returnMap.put(User.photo_path_ATTR, user.getphoto_path());
		// // returnUserSb.append(JSON.toJSONString(returnMap)).replace(0, 1,
		// // "[").replace(returnUserSb.length()-2, returnUserSb.length()-1,
		// // "]").append(",");
		// // System.out.println(i+":");
		// }
		// returnMap.put("userList", userList);
		// System.out.println(JSON.toJSONString(returnMap));

	}

}
